package com.situ.company.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebFilter("/*")
//专门管理是否登录的过滤器
public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest req =(HttpServletRequest) servletRequest;
        String uri =req.getRequestURI();//获取当前的请求地址

        //session会话



        boolean isLogin = req.getSession().getAttribute("currentUser") !=null;//看看有没有在session打过标记

        if(isLogin || uri.equals("/login")||uri.startsWith("/js/")||uri.startsWith("/images/")||uri.startsWith("/lib/")||uri.startsWith("/css/")) {
            filterChain.doFilter(servletRequest, servletResponse);//这行代码上边是请求进来时执行的，下边是往回返回时执行的（放行）
        }else {
            HttpServletResponse resp =(HttpServletResponse) servletResponse;
            resp.sendRedirect("/login");
        }
    }
}
